package com.example.company_project_fangyu.demo.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.company_project_fangyu.demo.common.LoginSet;
import com.example.company_project_fangyu.demo.utils.HttpClientUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static com.example.company_project_fangyu.demo.common.BasicData.*;

@Service
public class LibrariesService {

    @Autowired
    HttpClientUtils httpClientUtils;
    @Autowired
    LoginSet loginSet;


    /**
     * 添加人像底库
     */
    @SneakyThrows
    public JSON addFace(String name) {
        JSONObject params = new JSONObject();
        params.put("name",name);

        String url = BASIC_URL + "/api/open/libraries";

        String res = null;
        //发送请求
        try {
            res = httpClientUtils.doPostJson(url, params.toString(), loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return JSON.parseObject(res);

    }

    /**
     * 获取底库列表
     */
    public JSON getLibraryList() {
        String url = BASIC_URL + "/api/open/libraries/list";

        String res = null;
        //发送请求
        try {
            res = httpClientUtils.doGet(url,null,loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     *删除人像底库
     */
    public JSON deleteFace(Long id) {
        String url = BASIC_URL + "/api/open/libraries/" + id;

        String res = null;
        Map<String, String> headers = loginSet.getHeaders();
        //发送请求
        try {
            res = httpClientUtils.doDelete(url,headers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 修改人像底库
     */
    public JSON updateFace(Long id,String name) {
        JSONObject params = new JSONObject();
        params.put("id",id.toString());
        params.put("name",name);
        String url = BASIC_URL + "/api/open/libraries/";
        String res = null;
        try {
            res = httpClientUtils.doPutJson(url, params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 添加人像
     * @param image 图片base64二进制数据
     */
    public JSON addPerson(Long libraryId,String name,String image) {
        JSONObject params = new JSONObject();
        params.put("name",name);
        params.put("image",image);
        String url = BASIC_URL + "/api/open/persons/" + libraryId;
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url, params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 人像分页查询
     */
    public JSON getPersonList(Integer page,Integer pageSize,Long libId) {
        JSONObject params = new JSONObject();
        params.put("libId",libId.toString());
        params.put("pageIndex ",page.toString());
        params.put("pageSize",pageSize.toString());
        String url = BASIC_URL + "/api/open/libraries/person/page";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url,params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
   }

    /**
     * 修改人像
     */
    public JSON updatePerson(Long libraryId,String id,String name,String image) {
        JSONObject params = new JSONObject();
        params.put("id",id);
        params.put("name",name);
        params.put("image",image);
        String url = BASIC_URL + "/api/open/libraries/" + libraryId;
        String res = null;
        try {
            res = httpClientUtils.doPutJson(url, params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 删除人像
     */
    public JSON deletePerson(Long libraryId,String personId) {
        String url = BASIC_URL + "/api/open/libraries/" + libraryId + "/" + personId;
        String res = null;
        try {
            res = httpClientUtils.doDelete(url,loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 批量添加人像
     */
    public JSON batchAddPerson(Long libraryId,JSONArray params) {
        System.out.println(params);
        String url = BASIC_URL + "/api/open/libraries/" + libraryId + "/batchAdd";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url, params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
       return JSON.parseObject(res);
    }

    /**
     * 批量删除人像
     */
    public JSON batchDeletePerson(Long libraryId,String[] personIds) {
        JSONArray params = new JSONArray();
        params.addAll(Arrays.asList(personIds));
        String url = BASIC_URL + "/api/open/libraries/" + libraryId + "/batchDel";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url,params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }



}
